from django.core.paginator import Paginator
from django.views.decorators.http import require_POST, require_GET, require_http_methods

from utils.customer_util import CustomerUtil
from utils.response_code import ResponseCode
from .models import Role


# Create your views here.

def role_page(request):
    #  获取请求参数
    if request.method == 'POST':
        params = CustomerUtil.get_request_params(request)
        page = params.get('page', 1)
        size = params.get('size', 5)
        role_list = Role.objects.all().order_by('-id')
        paginator = Paginator(role_list, size)
        # 数据总条数
        total = paginator.count
        current_page = paginator.page(page)
        # [{},{},{}]
        table_data = list(current_page.object_list.values('id', 'role_zh', 'role_en', 'description'))

        return CustomerUtil.response_json({
            'code': ResponseCode.BUSINESS_SUCCESS,
            'message': '角色分页数据',
            'total': total,
            'table_data': table_data
        })
    else:
        return CustomerUtil.response_json({
            'code': ResponseCode.BUSINESS_ERROR,
            'message': '请求方式错误'
        })


def role_list(request):
    """角色列表查询  用于用户模块下拉选"""
    role_list = list(Role.objects.all().values('id', 'role_zh'))
    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        "roles": role_list
    })


def role_add(request):
    if request.method == 'POST':
        params = CustomerUtil.get_request_params(request)
        try:
            Role.objects.create(**params)
            response = {
                "code": ResponseCode.BUSINESS_SUCCESS,
                "message": "数据添加成功"
            }
        except Exception as e:
            response = {
                "code": ResponseCode.BUSINESS_ERROR,
                "message": "角色名或标识已存在"
            }

        return CustomerUtil.response_json(response)
    else:
        return CustomerUtil.response_json({
            'code': ResponseCode.BUSINESS_ERROR,
            'message': '请求方式错误'
        })


def role_edit(request):
    if request.method == 'POST':
        params = CustomerUtil.get_request_params(request)
        try:
            Role.objects.filter(id=params['id']).update(**params)
            response = {
                "code": ResponseCode.BUSINESS_SUCCESS,
                "message": "数据编辑成功"
            }
        except Exception as e:
            response = {
                "code": ResponseCode.BUSINESS_ERROR,
                "message": "角色名或标识已存在"
            }

        return CustomerUtil.response_json(response)
    else:
        return CustomerUtil.response_json({
            'code': ResponseCode.BUSINESS_ERROR,
            'message': '请求方式错误'
        })


def role_one(request, id):
    role = Role.objects.filter(id=id).values('id', 'role_zh', 'role_en', 'description').first()
    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        'role': role
    })


def role_delete(request, id):
    try:
        Role.objects.filter(id=id).delete()
        response = {
            "code": ResponseCode.BUSINESS_SUCCESS,
            "message": '数据删除成功'
        }
    except Exception as e:
        response = {
            "code": ResponseCode.BUSINESS_ERROR,
            "message": '角色被占用'
        }

    return CustomerUtil.response_json(response)
